package com.googlecode.messwertepi.gui;

import com.google.inject.Inject;
import com.googlecode.messwertepi.configuration.Configuration;
import com.googlecode.messwertepi.event.DataModelListener;
import com.googlecode.messwertepi.gui.DataLabel;
import com.googlecode.messwertepi.gui.TitledBorderPanel;
import com.googlecode.messwertepi.model.DataModel;
import jcurses.system.CharColor;
import jcurses.widgets.*;
import jcurses.widgets.List;

import java.util.*;

/**
 * Created by eike on 22.06.14.
 */
public class MainWindow implements DataModelListener
{
    private DataModel dataModel;

    private Window window;
    private DataLabel inputLabel;
    private List dataList;


    @Inject
    public MainWindow(Configuration configuration, DataModel dataModel)
    {
        this.dataModel = dataModel;

        // build the window
        int width = configuration.getGuiWidth();
        int height = configuration.getGuiHeight();

        buildWindow(width, height);

        // add the change listener
        dataModel.addDataModelListener(this);
    }

    final private void buildWindow(int width, int height)
    {
        window = new Window(width, height, true, "Messwerte lesen");
        Panel panel = window.getRootPanel();

        // add the top panel
        BorderLayoutManager inputLayoutManager = new BorderLayoutManager();
        BorderPanel inputPanel = new TitledBorderPanel(width, 3, "Input", new CharColor(CharColor.BLACK, CharColor.WHITE));
        inputPanel.setLayoutManager(inputLayoutManager);

        inputLabel = new DataLabel(dataModel.getInput(), new CharColor(CharColor.WHITE, CharColor.BLACK));
        inputLayoutManager.addWidget(inputLabel, BorderLayoutManager.CENTER, WidgetsConstants.ALIGNMENT_TOP, WidgetsConstants.ALIGNMENT_LEFT);

        // build the list
        dataList = new List(width);
        dataList.setTitle("Messwerte");
        dataList.setTitleColors(new CharColor(CharColor.BLACK, CharColor.WHITE));

        java.util.List<String> data = dataModel.getData();
        for (int i = 0; i < data.size(); i++) {
            String row = String.format("%1$4s", (i + 1) + ": h") + data.get(i);
            dataList.add(row);
        }

        // build the window
        BorderLayoutManager windowManager = new BorderLayoutManager();
        panel.setLayoutManager(windowManager);

        // Widget, Constaint, Vertical, Horizontal
        windowManager.addWidget(inputPanel, BorderLayoutManager.NORTH, WidgetsConstants.ALIGNMENT_TOP, WidgetsConstants.ALIGNMENT_LEFT);
        windowManager.addWidget(dataList, BorderLayoutManager.CENTER, WidgetsConstants.ALIGNMENT_TOP, WidgetsConstants.ALIGNMENT_LEFT);
    }

    /**
     * Shows the application window.
     */
    public void show()
    {
        window.show();
    }

    @Override
    public void inputChanged(String input) {
        inputLabel.setText(input);
    }

    @Override
    public void dataChanged(java.util.List<String> data) {
        int selectedItem = dataList.getSelectedIndex();

        dataList.clear();
        for (int i = 0; i < data.size(); i++) {
            String row = String.format("%1$-4s", (i + 1) + ":") + data.get(i);
            dataList.add(row);
        }
    }

    public void moveCursorDown()
    {
        changeCursorPosition(1);
    }

    public void moveCursorUp()
    {
        changeCursorPosition(-1);
    }

    private void changeCursorPosition(int delta)
    {
        int selectedRow = dataList.getSelectedIndex();
        int rowToSelect = Math.max(0, Math.min(dataList.getItemsCount() - 1, selectedRow + delta));

        dataList.select(rowToSelect);
    }
}